package com.education.admin.api.shiro;

import com.education.common.cache.CacheBean;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 * @author 余一
 * @Description ShiroBean配置类
 * @date 2021/6/27 17:43
 */
@Configuration
@AutoConfigureAfter(ShiroLifecycleBeanPostProcessorConfig.class)
public class ShiroBeanConfig {

    private static final long INVALID_TIME = 3600 * 6 * 1000;

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setUnauthorizedUrl("/unAuth");
        shiroFilterFactoryBean.setLoginUrl("/unAuth");
        LinkedHashMap<String,String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/login","anon");
        filterChainDefinitionMap.put("/image","anon");
        filterChainDefinitionMap.put("/system/**","authc");
        filterChainDefinitionMap.put("/logout","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        shiroFilterFactoryBean.setFilters(shiroFilterFactoryBean.getFilters());
        return shiroFilterFactoryBean;
    }
    @Bean
    public SecurityManager securityManager(SystemRealm systemRealm,CacheManager redisCacheManager){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(systemRealm);
        securityManager.setCacheManager(redisCacheManager);
//      securityManager.setCacheManager(ehcacheManager);
        return securityManager;
    }

    @Bean
    public SessionManager sessionManager(SessionDAO distributeShiroSession) {
        DefaultSessionManager sessionManager = new DefaultWebSessionManager();
        //设置session有效期
        sessionManager.setGlobalSessionTimeout(INVALID_TIME);
        sessionManager.setSessionDAO(distributeShiroSession);
        return sessionManager;
    }

    @Bean
    public RedisCacheManager redisCacheManager(CacheBean redisCacheBean){
        return new RedisCacheManager(redisCacheBean);
    }

    @Bean
    public SessionDAO distributeShiroSession(CacheBean redisCacheBean){
        return new DistributeShiroSession(redisCacheBean);
    }

//    @Bean
//    public CacheManager ehcacheManager(net.sf.ehcache.CacheManager cacheManager){
//        EhCacheManager ehCacheManager = new EhCacheManager();
//        ehCacheManager.setCacheManager(cacheManager);
//        return ehCacheManager;
//    }

//    @Bean
//    public net.sf.ehcache.CacheManager cacheManager(){
//        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
//        return net.sf.ehcache.CacheManager.create(inputStream);
//    }


    /**
     * 开启aop权限注解
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
